package com.qixin.modules.eaStandard.service.impl;

import com.qixin.modules.eaBasic.util.ChangeIdToNameUtil;
import com.qixin.modules.eaStandard.entity.EaStandardsetting;
import com.qixin.modules.eaStandard.mapper.EaStandardsettingMapper;
import com.qixin.modules.eaStandard.service.IEaStandardsettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: 报销控制标准设置
 * @Author: qixin-boot
 * @Date:   2021-07-17
 * @Version: V1.0
 */
@Service
public class EaStandardsettingServiceImpl extends ServiceImpl<EaStandardsettingMapper, EaStandardsetting> implements IEaStandardsettingService {
    @Autowired
    private EaStandardsettingMapper eaStandardsettingMapper;
    @Autowired
    private ChangeIdToNameUtil changeIdToNameUtil;

    /**
     * 保存
     * @param eaStandardsetting
     * @return map
     */
    public Map<String,Object> saveObject(EaStandardsetting eaStandardsetting){
        Map<String,Object> returnMap = new HashMap<String,Object>();

        //判断是否已经存在(相应的用户标准/相应的部门-职务)报销控制标准
        String companycode = eaStandardsetting.getCompanycode();   //组织

        //查询报销控制标准
        Map<String,Object> params = new HashMap<String,Object>();
        params.put("companycode",companycode);
        List<EaStandardsetting> eaStandardsettingList = eaStandardsettingMapper.selectByMap(params);
        if(eaStandardsettingList.size() > 0){
            returnMap.put("code","-1");
            returnMap.put("message","组织:" + changeIdToNameUtil.getNameByDepartId(companycode) + "已经维护报销控制标准,不要重复维护!");
            return returnMap;
        }else{
            eaStandardsettingMapper.insert(eaStandardsetting);

            returnMap.put("code","0");
            returnMap.put("message","新增成功!");
            return returnMap;
        }
    }

    /**
     * 编辑
     * @param eaStandardsetting
     * @return map
     */
    public Map<String,Object> updateByIdObject(EaStandardsetting eaStandardsetting){
        Map<String,Object> returnMap = new HashMap<String,Object>();

        //判断是否已经存在(相应的用户标准/相应的部门-职务)报销控制标准
        String companycode = eaStandardsetting.getCompanycode();   //组织

        //查询报销控制标准
        Map<String,Object> params = new HashMap<String,Object>();
        params.put("companycode",companycode);
        List<EaStandardsetting> eaStandardsettingList = eaStandardsettingMapper.selectByMap(params);
        if(eaStandardsettingList.size() > 0){
            //查询当前标准
            EaStandardsetting eaStandardsetting1 = eaStandardsettingMapper.selectById(eaStandardsetting.getId());
            if(eaStandardsetting1.getCompanycode().equals(companycode)){  //如果当前标准维护的组织没变,返回成功
                eaStandardsettingMapper.updateById(eaStandardsetting);

                returnMap.put("code","0");
                returnMap.put("message","编辑成功");
                return returnMap;
            }else{
                returnMap.put("code","-1");
                returnMap.put("message","组织:" + changeIdToNameUtil.getNameByDepartId(companycode) + "已经维护报销控制标准,不要重复维护!");
                return returnMap;
            }
        }else{
            eaStandardsettingMapper.updateById(eaStandardsetting);

            returnMap.put("code","0");
            returnMap.put("message","编辑成功!");
            return returnMap;
        }
    }

}
